#程序文件ex10_1_2.py
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt

d = np.loadtxt('data10_1.txt')
x0 = d[0]
y0 = d[1]
d = {'x':x0, 'y':y0}
re = sm.formula.ols('y~x', d).fit() #拟合线性回归模型

print(re.summary())
print(re.outlier_test())  #输出已知数据的野值检验
print('残差的方差', re.mse_resid)
pre = re.get_prediction(d)
df = pre.summary_frame(alpha=0.05)
dfv = df.values 
low, upp = dfv[:,4:].T #置信下限上限
r = (upp-low)/2  #置信半径
num = np.arange(1, len(x0)+1)

fig=plt.Figure()
ax=fig.add_subplot(111)
ax.plot(x0,y0,'o')
ax.plot(x0,dfv[:,0],'r-')
ax.plot(x0,dfv[:,4],'b--')
ax.plot(x0,dfv[:,5],'b--')

plt.errorbar(num, re.resid, r, fmt='o')
#plt.show()

'''
x = np.array([0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18,
         0.2, 0.22, 0.24])
y = np.array([42., 42.5, 45., 45.5, 45., 47.5, 49., 51., 50., 
         55., 57.5, 59.5])
'''


